package com.zsy.controller;

import com.zsy.dto.SetmealDto;
import com.zsy.pojo.Page;
import com.zsy.pojo.R;
import com.zsy.pojo.Setmeal;
import com.zsy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        //补全创建更新者信息
        long result = (long) session.getAttribute("result");
        setmealDto.setCreateUser(result);
        setmealDto.setUpdateUser(result);
        //调用方法保存
        setmealService.save(setmealDto);

        return R.success("保存信息");
    }
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> setmealList = setmealService.findByPage(page,pageSize,name);

        return R.success(setmealList);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findById(id);

        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto,HttpSession session){
        Long result = (Long) session.getAttribute("result");

        setmealDto.setUpdateUser(result);

        setmealService.update(setmealDto);

        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);

        return R.success("删除成功");
    }

    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId, Integer status){

        return R.success(setmealService.list(categoryId,status));


    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus( @PathVariable("status") Integer status , @RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);

        return R.success("修改成功");

    }



}
